<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<script src="https://unpkg.com/immer@6.0.3/dist/immer.umd.production.min.js"></script>
<script>
  immer.enableMapSet()

  const usersById_v1 = new Map([
    ["michel", {name: "Michel Weststrate", country: "NL"}]
  ])

  const usersById_v2 = immer.produce(usersById_v1, draft => {
    draft.get("michel").country = "UK"
  })

  // console.log(usersById_v2.get("michel").country)
  // console.log(usersById_v1.get("michel").country)
  // console.log(usersById_v1 === usersById_v2)

  function createFunc(a, b) {
    const funcString = 'return a + b';

    return new Function('a', 'b', funcString);
  }

  const func = createFunc(1, 2);

  console.log(func);

  function createFunc2(path){
    let func = `draft`;

    while (path.length) {
      const index = path.shift()

      func += `[${ +index }].children`
    }

    // dart[index].children = callback(${func})
    func += ` = callback(${func})`

    return new Function('draft', 'callback', func)
  }

  const func2 = createFunc2([1, 2]);

  console.log(func2);

</script>
</body>
</html>
